//
//  ViewController.m
//  Test
//
//  Created by Hunk on 12-1-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import "SCListener.h" 

@implementation ViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    
//    // Start listening.
//    [[SCListener sharedListener] listen];
//    
//    // Retrieve the average power.
//    [[SCListener sharedListener] averagePower];
//    
//    // Retrieve the peak power.
//    [[SCListener sharedListener] peakPower];
    
    // Hmm...we're using this guy a lot...
    SCListener *listener = [SCListener sharedListener];
    
    // We can temporarily stop returning levels
//    [listener pause];
    [listener listen]; // Quick.
    
    
    
    // Or free up resources when we're not listening for awhile.
//    [listener stop];
//    [listener listen]; // Slower.
    
    // Advanced!:
    //
    // If you're using the average and the peak, fetch both at once.
//    if (![listener isListening]) // If listener has paused or stopped...
//        return;                    // ...bail.
//    
//    AudioQueueLevelMeterState *levels = [listener levels];
//    Float32 peak = levels[0].mPeakPower;
//    Float32 average = levels[0].mAveragePower;

    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(100, 100, 100, 40)];
    [button setTitle:@"Tap" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonHandle:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    timer = [NSTimer timerWithTimeInterval:1.0/60 target:self selector:@selector(buttonHandle:) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

- (void)buttonHandle:(id)sender
{
    CGFloat ff = [[SCListener sharedListener] averagePower];
    
    if(ff > 0.5)
    {
        NSLog(@"------------------------------================%f\n", ff);
    }
    else
    {
        NSLog(@"%f\n", ff);
    }  
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
